| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256 |
1
1
1
5
5
4
4
5
4
1
4
3
3
3
29
2
1
1
1
29
2
1
1
1
29
2
1
1
1
1
2
2
1
1
6
2
4
5
5
5
3
2
2
2
1
5
5
3
5
5
2
2
2
2
2
2
2
2
2
2
2
1
1
9
9
9
9
9
9
9
9
9
9
1
1
1
1
1
1
1
1
1
1
3
3
3
2
1
1
1
1
2
1
| var _ = require('../utility');
var errorParser = require('./errorParser');
var logger = require('./logger');
function handleItemWithError(item, options, callback) {
item.data = item.data || {};
if (item.err) {
try {
item.stackInfo = item.err._savedStackTrace || errorParser.parse(item.err);
} catch (e)
/* istanbul ignore next */
{
logger.error('Error while parsing the error object.', e);
item.message = item.err.message || item.err.description || item.message || String(item.err);
delete item.err;
}
}
callback(null, item);
}
function ensureItemHasSomethingToSay(item, options, callback) {
if (!item.message && !item.stackInfo && !item.custom) {
callback(new Error('No message, stack info, or custom data'), null);
}
callback(null, item);
}
function addBaseInfo(item, options, callback) {
var environment = (options.payload && options.payload.environment) || options.environment;
item.data = _.extend(true, {}, item.data, {
environment: environment,
level: item.level,
endpoint: options.endpoint,
platform: 'browser',
framework: 'browser-js',
language: 'javascript',
server: {},
uuid: item.uuid,
notifier: {
name: 'rollbar-browser-js',
version: options.version
}
});
callback(null, item);
}
function addRequestInfo(window) {
return function(item, options, callback) {
if (!window || !window.location) {
return callback(null, item);
}
_.set(item, 'data.request', {
url: window.location.href,
query_string: window.location.search,
user_ip: '$remote_ip'
});
callback(null, item);
};
}
function addClientInfo(window) {
return function(item, options, callback) {
if (!window) {
return callback(null, item);
}
_.set(item, 'data.client', {
runtime_ms: item.timestamp - window._rollbarStartTime,
timestamp: Math.round(item.timestamp / 1000),
javascript: {
browser: window.navigator.userAgent,
language: window.navigator.language,
cookie_enabled: window.navigator.cookieEnabled,
screen: {
width: window.screen.width,
height: window.screen.height
}
}
});
callback(null, item);
};
}
function addPluginInfo(window) {
return function(item, options, callback) {
if (!window || !window.navigator) {
return callback(null, item);
}
var plugins = [];
var navPlugins = window.navigator.plugins || [];
var cur;
for (var i=0, l=navPlugins.length; i < l; ++i) {
cur = navPlugins[i];
plugins.push({name: cur.name, description: cur.description});
}
_.set(item, 'data.client.javascript.plugins', plugins);
callback(null, item);
};
}
function addBody(item, options, callback) {
if (item.stackInfo) {
addBodyTrace(item, options, callback);
} else {
addBodyMessage(item, options, callback);
}
}
function addBodyMessage(item, options, callback) {
var message = item.message;
var custom = item.custom;
if (!message) {
if (custom) {
var scrubFields = options.scrubFields;
var messageResult = _.stringify(_.scrub(custom, scrubFields));
message = messageResult.error || messageResult.value || '';
} else {
message = '';
}
}
var result = {
body: message
};
if (custom) {
result.extra = _.extend(true, {}, custom);
}
_.set(item, 'data.body', {message: result});
callback(null, item);
}
function addBodyTrace(item, options, callback) {
var description = item.data.description;
var stackInfo = item.stackInfo;
var custom = item.custom;
var guess = errorParser.guessErrorClass(stackInfo.message);
var className = stackInfo.name || guess[0];
var message = guess[1];
var trace = {
exception: {
'class': className,
message: message
}
};
if (description) {
trace.exception.description = description;
I}
// Transform a TraceKit stackInfo object into a Rollbar trace
var stack = stackInfo.stack;
if (stack && stack.length === 0 && item._unhandledStackInfo && item._unhandledStackInfo.stack) {
stack = item._unhandledStackInfo.stack;
I}
if (stack) {
var stackFrame;
var frame;
var code;
var pre;
var post;
var contextLength;
var i, mid;
trace.frames = [];
for (i = 0; i < stack.length; ++i) {
stackFrame = stack[i];
frame = {
filename: stackFrame.url ? _.sanitizeUrl(stackFrame.url) : '(unknown)',
lineno: stackFrame.line || null,
method: (!stackFrame.func || stackFrame.func === '?') ? '[anonymous]' : stackFrame.func,
colno: stackFrame.column
};
if (frame.method && frame.method.endsWith && frame.method.endsWith('._rollbar_wrapped')) {
continue;
I}
code = pre = post = null;
contextLength = stackFrame.context ? stackFrame.context.length : 0;
if (contextLength) {
mid = Math.floor(contextLength / 2);
I pre = stackFrame.context.slice(0, mid);
code = stackFrame.context[mid];
post = stackFrame.context.slice(mid);
}
if (code) {
frame.code = code;
I}
if (pre || post) {
frame.context = {};
I if (pre && pre.length) {
frame.context.pre = pre;
}
if (post && post.length) {
frame.context.post = post;
}
}
if (stackFrame.args) {
frame.args = stackFrame.args;
I}
trace.frames.push(frame);
}
// NOTE(cory): reverse the frames since rollbar.com expects the most recent call last
trace.frames.reverse();
if (custom) {
trace.extra = _.extend(true, {}, custom);
E}
_.set(item, 'data.body', {trace: trace});
callback(null, item);
} else {
item.message = className + ': ' + message;
addBodyMessage(item, options, callback);
}
}
function scrubPayload(item, options, callback) {
var scrubFields = options.scrubFields;
_.scrub(item.data, scrubFields);
callback(null, item);
}
function userTransform(item, options, callback) {
var newItem = _.extend(true, {}, item);
try {
if (_.isFunction(options.transform)) {
options.transform(newItem.data);
}
} catch (e) {
options.transform = null;
logger.error('Error while calling custom transform() function. Removing custom transform().', e);
callback(null, item);
return;
}
callback(null, newItem);
}
module.exports = {
handleItemWithError: handleItemWithError,
ensureItemHasSomethingToSay: ensureItemHasSomethingToSay,
addBaseInfo: addBaseInfo,
addRequestInfo: addRequestInfo,
addClientInfo: addClientInfo,
addPluginInfo: addPluginInfo,
addBody: addBody,
scrubPayload: scrubPayload,
userTransform: userTransform
};
|